/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.service.PatientService;
import java.util.Date;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author Administrator
 */
public class AddPatient extends GenericAutowireComposer {

    private Textbox fullName;
    private Datebox birthdate;
    private Textbox address;
    private Textbox phone;
    private Textbox job;
    private Window self;
    private PatientService patientService = util.Utility.getUtility().getPatientService();

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        setConstraint();
    }

    private void setConstraint() {
        fullName.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateFullname(cmpnt, o);
            }
        });
        birthdate.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateBirthDate(cmpnt, o);
            }
        });
        address.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateAddress(cmpnt, o);
            }
        });
        phone.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validatePhone(cmpnt, o);
            }
        });
        job.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateJob(cmpnt, o);
            }
        });
    }

    private void validateFullname(Component comp, Object value) throws WrongValueException {
        String s = (String) value;
        s = s.trim();
        if (s.length() < 1) {
            throw new WrongValueException(comp, "Fullname cannot be empty");
        }
    }

    private void validateJob(Component comp, Object value) throws WrongValueException {
        String s = (String) value;
        s = s.trim();
        if (s.length() < 1) {
            throw new WrongValueException(comp, "Job cannot be empty");
        }
    }

    private void validateBirthDate(Component comp, Object value) throws WrongValueException {
        Date d = (Date) value;
        if (d == null) {
            throw new WrongValueException(comp, "Birthdate cannot be empty");
        }
        if (d.after(new Date())) {
            throw new WrongValueException(comp, "Birthdate must be less than current date!!!");
        }
    }

    private void validateAddress(Component comp, Object value) throws WrongValueException {
        String s = (String) value;
        s = s.trim();
        if (s.length() < 1) {
            throw new WrongValueException(comp, "Address cannot be empty");
        }
    }

    private void validatePhone(Component comp, Object value) throws WrongValueException {
        String s = (String) value;
        s = s.trim();
        if (s.length() < 1) {
            throw new WrongValueException(comp, "Phone cannot be empty");
        }
    }

    public void onReset(Event e) {
        fullName.setValue("");
        address.setValue("");
        phone.setValue("");
        birthdate.setValue(new Date());
    }

    public void onClose(Event e) {
        self.onClose();
        Executions.sendRedirect("TrackPatient.do");
    }

    public void onSubmit(Event e) {
        try {
            patientService.createPatient(fullName.getValue(), birthdate.getValue(),
                    job.getValue(), address.getValue(), phone.getValue());
            Messagebox.show("New patient info has been inserted!", "Information", Messagebox.OK, Messagebox.INFORMATION);
            self.onClose();
            Executions.sendRedirect("TrackPatient.do");
        } catch (InterruptedException ex) {
            System.out.println(ex.getMessage());
        }
    }
}
